package com.mxy.myapplication.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Environment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.BitmapEncoder;
import com.mxy.myapplication.R;
import com.mxy.myapplication.activity.ActivityForCutp;
import com.mxy.myapplication.pojo.Pojo1;

import java.util.ArrayList;

/**
 * 线性布局RecyclerView适配器
 */

public class AdapterForRv_Linear extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context cont;
    private LayoutInflater lf;
    private ArrayList<Pojo1> data;

    public AdapterForRv_Linear(Context cont,ArrayList<Pojo1> data){
        this.cont=cont;
        this.lf=LayoutInflater.from(cont);
        this.data=data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(this.lf.inflate(R.layout.item_recyclerview_linear,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myViewHolder=(MyViewHolder) holder;
        myViewHolder.iv.setOnClickListener(new onclickListener(position));

        Glide.with(cont)
                .load(data.get(position).drawableID)
                .asBitmap()
                .encoder(new BitmapEncoder(Bitmap.CompressFormat.PNG,100))
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .dontAnimate()
                .animate(R.anim.alpha)
                .into(myViewHolder.iv);

        if(data.get(position).stickyHeader_type==1){
            myViewHolder.stickyheaderview.setVisibility(View.VISIBLE);
            myViewHolder.stickyheaderview.setText(data.get(position).stickyHeaderContent);
        }else{
            myViewHolder.stickyheaderview.setVisibility(View.GONE);
        }

        myViewHolder.itemView.setTag(data.get(position).stickyHeader_type);//保存是否该类型头一个item
        myViewHolder.itemView.setContentDescription(data.get(position).stickyHeaderContent);//保存item所属标签内容
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    /**
     * 自定义ViewHolder
     */
    class MyViewHolder extends RecyclerView.ViewHolder{

        ImageView iv;
        TextView stickyheaderview;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv=(ImageView) itemView.findViewById(R.id.imageview);
            stickyheaderview=(TextView) itemView.findViewById(R.id.stickyheaderview);
        }
    }

    /**
     * 为item增加点击事件
     */
    class onclickListener implements View.OnClickListener{

        int position;

        public onclickListener(int position){
            this.position=position;
        }

        @Override
        public void onClick(View v) {

            System.out.println(Environment.getDataDirectory().getPath());
            System.out.println(Environment.getDownloadCacheDirectory().getPath());
            System.out.println(cont.getCacheDir().getPath());
            System.out.println(cont.getFilesDir().getPath());

            System.out.println(Environment.getExternalStorageDirectory().getPath());
            System.out.println(Environment.getExternalStoragePublicDirectory("path").getPath());
            System.out.println(cont.getExternalCacheDir().getPath());
            System.out.println(cont.getExternalFilesDir("photo/my").getPath());

            cont.startActivity(new Intent(cont, ActivityForCutp.class));
        }
    }
}